package com.xiaohai.wx.weather.util;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.springframework.util.Assert;

import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UrlUtils {
    private static final String defaultPrefix = "${:";
    private static final String defaultSuffix = "}";
    private static Map<Character,String> validCode=new HashMap<Character, String>();

    static {
        validCode.put('.',"\\.");
        validCode.put('$',"\\$");
        validCode.put('^',"\\^");
        validCode.put('{',"\\{");
        validCode.put('[',"\\[");
        validCode.put('(',"\\(");
        validCode.put(')',"\\)");
        validCode.put('\\',"\\\\");
        validCode.put('*',"\\*");
        validCode.put('+',"\\+");
        validCode.put('|',"\\|");
        validCode.put('?',"\\?");
    }
    public static String toRegexSafe(String str){
        str=StringUtils.defaultString(str);
        char[] chars=str.toCharArray();
        StringBuilder builder=new StringBuilder();
        for(char ch:chars){
            String c =(validCode.keySet().contains(ch)?validCode.get(ch):String.valueOf(ch));
            builder.append(c);
        }
        return builder.toString();
    }
    public static Map<String, String> getQueryParam(String url) throws MalformedURLException {
        Assert.isTrue(StringUtils.isNotBlank(url), "getQUeryParam url不能为空");
        Map<String, String> paramMap = new HashMap<String, String>();
        List<NameValuePair> params = URLEncodedUtils.parse(url, Charset.forName("UTF-8"));
        for (NameValuePair nv : params) {
            paramMap.put(nv.getName(), nv.getValue());
        }
        return paramMap;
    }

    public static String replaceNamedParam(String url, Map<String, String> params) {
        return replaceNamedParam(url, params, defaultPrefix, defaultSuffix);
    }

    public static String replaceNamedParam(String url, Map<String, String> params, String prefix, String suffix) {
        Assert.isTrue(!StringUtils.startsWith(prefix,"\\"), "prefix can't be an invalid char");
        int hostEnd=url.indexOf("?");
        if(hostEnd==-1){
            return url;
        }
        String host=url.substring(0,hostEnd+1);
        url=url.substring(hostEnd+1);
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> param : params.entrySet()) {
                String var = toRegexSafe(prefix) + param.getKey() + toRegexSafe(suffix);
                url=url.replaceAll(var,param.getValue());
            }
        }
        return host+url;
    }
    public static void main(String[] args){
        String url="www.baidu.com?name=${:name}&age=${:age}";
        Map<String,String> params=new HashMap<String, String>();
        params.put("name","张三");
        params.put("age","24");
        System.out.println(UrlUtils.replaceNamedParam(url,params));
    }
}
